﻿var BizDatagrid = function (dgselector, dgsetts, rightSelector) {
    this.selector = dgselector;
    this.source = $(this.selector);
    var $grid = this.source;
    dgsetts = dgsetts || {};
    if (dgsetts.url) {
        dgsetts.url = AppendTransfers(dgsetts.url);
    }
    rightSelector = rightSelector || ".right-tool";
    var defaultSetts = {
        onRowContextMenu: function (e, rowIndex, rowData) {
            e.preventDefault(); //阻止浏览器捕获右键事件
            $grid.datagrid("clearSelections"); //取消所有选中项
            $grid.datagrid("selectRow", rowIndex); //根据索引选中该行
            $(rightSelector).menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    };
    $.extend(true, defaultSetts, dgsetts);
    $grid.datagrid(defaultSetts);
};
BizDatagrid.prototype =
{
    add: function (url) {
        TransferToUrl(url);
    },
    edit: function (url, keyName, paramName) {
        paramName = paramName || "id";
        keyName = keyName || "Id";
        var objRow = this.selectRow();
        if (objRow) {
            if (url.indexOf("?") > 0) {
                url = url + "&" + paramName + "=" + objRow[keyName];
            } else {
                url = url + "?" + paramName + "=" + objRow[keyName];
            }
            TransferToUrl(url);
        } else {
            ShowMessage("请先选择一条数据");
        }
    },
    search: function (frselector, url) {
        var objFormEntity = new FormEntity(frselector);
        var $grid = this.source;
        objFormEntity.handle = function () {
            $grid.datagrid({
                url: AppendTransfers(url),
                queryParams: objFormEntity.formData
            });
        };
        objFormEntity.submit();
    },
    del: function (url, keyName, successFunc) {
        var $grid = this.source;
        var objRows = $grid.datagrid('getSelections');
        var objIds = [];
        if (!objRows || objRows.length == 0) {
            ShowMessage("请先选择一条数据");
            return;
        }
        keyName = keyName || "Id";
        ConfirmMessage("确认删除吗", function (isConfirm) {
            if (!isConfirm) {
                return;
            }
            $.each(objRows, function (i, objRow) {
                objIds.push(objRow[keyName]);
            });
            var objAjaxEntity = new AjaxEntity();
            objAjaxEntity.url = AppendTransfers(url);
            objAjaxEntity.data = { id: objIds.join() };
            objAjaxEntity.executeSuccess = function (InvokeResult) {
                if (InvokeResult.ResultCode == "Success") {
                    ShowMessage(InvokeResult.ResultMessage);
                    $grid.datagrid('reload');
                    if (successFunc) {
                        successFunc();
                    }
                }
            };
            objAjaxEntity.execute();
        });
    },
    reload: function () {
        this.source = $(this.selector);
        var $grid = this.source;
        $grid.datagrid("reload");
    },
    selectRow: function () {
        return this.source.datagrid('getSelected');
    },
    selectRows: function () {
        return this.source.datagrid('getSelections');
    },
    //若url带QuaryString参数，表单必须为Post提交，才能带上QuaryString参数
    export: function (frselector, url) {
        //$.form('submit', { url: url })  若url带QuaryString参数，表单必须为Post提交，才能带上QuaryString参数
        var objFormEntity = new FormEntity(frselector);
        objFormEntity.url = url;
        objFormEntity.handle = function () {
            var $this = this;
            this.source.form('submit', { url: $this.url });
        }
        objFormEntity.submit();
    }
};